define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tenmillionproject/index' + location.search,
                    add_url: 'tenmillionproject/add',
                    edit_url: 'tenmillionproject/edit',
                    del_url: 'tenmillionproject/del',
                    multi_url: 'tenmillionproject/multi',
                    table: 'tenmillionproject',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: '项目名称', operate:'like'},
                        {field: 'scope_business', title: __('经营范围'),operate:false,formatter: function(value,row,index) {
                                var str = value?value:'';
                                return '<div title="'+str+'" style="width:150px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">'+str+'</div>';
                            }},
                        {field: 'investment_name', title: '投资方名称', operate:'like'},
                        {field: 'investment_area', title: '投资方地区', operate:'like'},
                        {field: 'is_out', title: '是否是市外项目',searchList:{"是":"是","否":"否"}},
                        {field: 'register_time', title: __('Register_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetimedate},
                        {field: 'investment_total', title: __('Investment_total'), operate:'BETWEEN'},
                        {field: 'registered_capital', title: __('Registered_capital'), operate:'BETWEEN'},
                        {field: 'industry_type_name', title: __('Industry_type_name')},
                        {field: 'street_name', title: __('所属街道'),operate: 'like'},
                        {field: 'area', title: __('占地或面积'),operate: 'like'},
                        {field: 'progress_status', title: __('进展状态'),operate: 'like'},
                        {field: 'status',title:'状态',operate:false,
                            formatter: function (value,row,index) {
                                if(value == 0) {
                                    return '<a href="javascript:;" class="btn btn-success btn-xs ">已审核</a>';
                                }else if(value == 100)
                                {
                                    return '<a href="tenmillionproject/checkedit/ids/' + row.id + '" class="btn btn-danger   btn-addtabs btn-xs" title="审核">待审核</a>';
                                }else if(value == 200){
                                    return '<a href="javascript:;" data-info="'+row.reject_reason+'" class="btn btn-warning btn-xs btn-rejectinfo">已驳回</a>';
                                }else if(value == '300'){
                                    return '<a href="javascript:;" class="btn btn-info btn-edit btn-xs">待提交</a>';
                                }

                            }
                        },
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            $("#table").on("click",'.btn-rejectinfo',function (table){
                Layer.open({
                    type: 1,
                    title: __('查看驳回原因'),
                    area: ["500px", "400px"],
                    content: $(this).data('info'),
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            })


            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".btn-passall").click(function (){

                Layer.confirm(__('确定要通过所有吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "tenmillionproject/passall" ,
                        success: function (ret) {
                            Layer.alert(ret.msg, function(index){
                                Layer.closeAll();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.closeAll();
                });
            });

            $("#btn-import-custom").click(function () {
                if(Config.isbinddep || Config.isSuperAdmin)
                {
                    var url = 'tenmillionproject/import';
                    Fast.api.open(url, __('Import'), {});
                } else {
                    Layer.alert(__('您还未绑定部门，请先绑定'));
                }

            });

            var parenttable = table.closest('.bootstrap-table');
            var options = table.bootstrapTable('getOptions');
            var toolbar = $(options.toolbar, parenttable);

            $(toolbar).on('click', ".btn-export", function () {
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});
                var filter = search.filter;
                var op = search.op;
                var url = '/NIaBskCwTF.php/tenmillionproject/export';

                var ids = Table.api.selectedids(table);
                ids = ids.join(",")
                if(ids){
                    url = url+'/ids/'+ids;
                }
                location.href= url + '?filter='+filter+'&op='+op;
            });
        },
        import:function() {
            Controller.api.bindevent();

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();


            $(".dot").click(function(event) {
                var that = this;
                var tenmillionproject_id = $(that).data("ids");
                var field = $(that).data("field");
                // var op = '{"tenmillionproject_id":"=","field":"="}';
                // var filter = '{"tenmillionproject_id":"'+tenmillionproject_id+'","field":"'+field+'"}';


                var url ='tenmillionproject/checklog/ids/' + tenmillionproject_id +'/field/'+ field ;
                console.log(url);
                // Backend.api.addtabs(url,'修改记录')
                // window.open(url, "_newtab");
                Fast.api.open(url, '', {area:['95%','95%']})
            })
        },
        checkedit: function(){
            $("#btn-pass").click(function () {
                var ids = $(this).data("ids");
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "tenmillionproject/checkpass" ,
                    data: {ids:ids},
                    success: function (ret) {
                        Layer.alert(__('提交成功'), function(index){
                            location.reload();
                        });
                    },
                    error : function(error) {
                        console.log(error);
                    }
                });
            });

            $("#btn-reject").click(function () {
                var ids = $(this).data("ids");
                Fast.api.open('tenmillionproject/checkreject/ids/'+ids, '驳回', {area: ["650px", "300px"]});
            });
            Controller.api.bindevent();
        },
        checkreject: function (){
            $("#btn-reject").click(function () {
                var ids = $(this).data("ids");
                var reject_reason = $("#reject_reason").val();

                Layer.confirm(__('确定要驳回吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "tenmillionproject/checkreject" ,
                        data: {ids:ids,reject_reason:reject_reason},
                        success: function (ret) {
                            Layer.alert(__('驳回成功'), function(index){
                                parent.location.reload();
                            });
                        },
                        error : function(error) {
                            console.log(error);
                        }
                    });
                    Layer.closeAll();
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});